package com.cqc.asycnhttpdemo01;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.orhanobut.logger.Logger;

import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "PRETTYLOGGER";
    private ProgressDialog dialog;
    private Context context = this;
    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.btn3)
    Button btn3;
    @BindView(R.id.btn4)
    Button btn4;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.btn5)
    Button btn5;
    @BindView(R.id.btn6)
    Button btn6;
    @BindView(R.id.btn7)
    Button btn7;
    @BindView(R.id.btn8)
    Button btn8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        dialog = new ProgressDialog(context);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setCancelable(false);
    }

    @OnClick({R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                httpGet();
                break;
            case R.id.btn2:
                httpPost();
                break;
            case R.id.btn3:
                upLoad();
                break;
            case R.id.btn4:
                downLoad();
                break;
            case R.id.btn5:
                httpGet2();
                break;
            case R.id.btn6:
                httpPost2();
                break;
        }
    }

    private void httpPost2() {
        RequestParams params = new RequestParams();
        params.add("name","AndroidCQC");
        params.add("password","123456");
        AsyncHttpUtil.post("aa/bb", null, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }

    private void httpGet2() {
        AsyncHttpUtil.get("aa/bb", null, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }

    private void downLoad() {
        String url = "http://192.168.1.11:8080/QQ/QQ.apk";
        AsyncHttpClient http = new AsyncHttpClient();
        http.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                dialog.setTitle("提示信息");
                dialog.setMessage("下载中...");
                dialog.show();
            }

            @Override
            public void onProgress(final long bytesWritten, final long totalSize) {
                super.onProgress(bytesWritten, totalSize);
                dialog.setMax((int) totalSize);
                dialog.setProgress((int) bytesWritten);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                dialog.dismiss();
                Logger.d("onFailure=下载成功");
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                String result = responseBody.toString();
                dialog.dismiss();
                Logger.d("onFailure=下载失败");
            }
        });
    }

    private void upLoad() {
        String url = "http://192.168.1.11:8080/okhttp/json3";
        AsyncHttpClient http = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        File file = new File("/mnt/sdcard0/qq.apk");
        try {
            params.put("file", file);
            http.get(url, params, new JsonHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    super.onSuccess(statusCode, headers, response);
                    Logger.d("上传成功");
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    super.onFailure(statusCode, headers, responseString, throwable);
                    Logger.d("上传失败");
                }
            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void httpPost() {
        String url = "http://apicloud.mob.com/v1/mobile/address/query";
        AsyncHttpClient http = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.add("key", "1922d51a5257d");//put(...)可以刻传递参数
        params.add("phone", "15153519007");

        http.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String result = new String(responseBody);
                Logger.d("onSuccess=" + result);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                String result = responseBody.toString();
                Logger.d("onFailure=" + result);
            }
        });
    }

    private void httpGet() {

//        String url = "http://apicloud.mob.com/v1/mobile/address/query?key=1922d51a5257d&phone=15153519007";
        String url = "http://192.168.1.11:8080/okhttp/json3";
        AsyncHttpClient http = new AsyncHttpClient();
        http.get(url, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String result = response.toString();
                Logger.d("onSuccess=" + result);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Logger.d("onFailure=" + responseString);
            }
        });

    }

}
